package com.lucher.tangstore.common;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

/**
 * 判断登录界面session失效
 */
public class BaseInterceptor extends HandlerInterceptorAdapter {

	Logger logger = LoggerFactory.getLogger(BaseInterceptor.class);

	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {

		logger.info("--进入--拦截器--");

		if (request.getRequestURI() == null) {
			request.getRequestDispatcher("/tangstore/").forward(request, response);

			logger.info("--request.getRequestURI() is null--");


			return false;
		}
		if (("/tangstore/").equals(request.getRequestURI())) {

			logger.info("--request.getRequestURI()=" + request.getRequestURI());

			response.sendRedirect(request.getContextPath() + "/login");
			return false;
		}

		if (!(request.getRequestURI().endsWith("/tangstore/login")
				|| request.getRequestURI().endsWith("/tangstore/goLogin")
				|| request.getRequestURI().endsWith("/tangstore/logout")
				|| request.getRequestURI().endsWith("/tangstore/changeLanguage")
				|| request.getRequestURI().endsWith("/tangstore/noPermission")
				|| request.getRequestURI().indexOf("tangstore/js") > -1
				|| request.getRequestURI().indexOf("tangstore/layuiadmin") > -1
				|| request.getRequestURI().indexOf("tangstore/sale") > -1
				|| request.getRequestURI().indexOf("tangstore/style") > -1
				|| request.getRequestURI().indexOf("tangstore/images") > -1
				|| request.getRequestURI().indexOf("tangstore/httpForMps") > -1
				|| request.getRequestURI().indexOf("tangstore/httpForMachine") > -1
				|| request.getRequestURI().indexOf("tangstore/screanshow") > -1
				|| request.getRequestURI().indexOf("tangstore/xxq") > -1  //url 测试

				|| request.getRequestURI().indexOf("codearea") > -1)) {
			
			if (request.getSession().getAttribute("Operator") == null) {
				response.sendRedirect(request.getContextPath()+"/noPermission"); 
				return false;
			}
		}
		return super.preHandle(request, response, handler);
	}
}
